package fixtures;

import java.awt.Component;
import java.io.IOException;
import clientGUI.LoginPanel;
import clientGUI.MainGUI;
import clientGUI.SubscriberGUI;

import subscriberPanels.UpdateDetailsPanel;
import fit.ActionFixture;

public class UpdateDetails extends ActionFixture 
{

	MainGUI main;
	LoginPanel lg;
	UpdateDetailsPanel pn;
	String userName;
	String pw;
	SubscriberGUI sg;
	String newFirstName;
	String newLastName;
	String email;
	String FirstName;
	String LastName;
	String check;
	String phone;
	String id;
	
	String newPassword;
	String oldPassword;


	
	public void initGui() throws IOException
	{
		main=new MainGUI("localhost",5555);
		MainGUI.testmode=true;
	}
	
	public synchronized void startConnection() {
		lg=new LoginPanel();
		lg.textFieldLogin.setText(userName);
		String temp=lg.textFieldLogin.getText();
		lg.passwordField.setText(pw);
		MainGUI.done=false;
		lg.btnSubmit.doClick();
		while(MainGUI.done==false);
		Component x[]=MainGUI.panelButtons.getComponents();
		int y=MainGUI.panelChanging.getComponentCount();
		MainGUI.panelButtons.toString();
		MainGUI.done=false;
		sg=((SubscriberGUI)x[0]);
		sg.btnUpdateDetails.doClick();
		while(MainGUI.done==false);
		pn=(UpdateDetailsPanel) MainGUI.panelChanging.getComponent(0);
	}
	public void logout() throws IOException {
		sg.btnLogout.doClick();
		while(MainGUI.done==false);
	}
	public void insertUserName(String name) {
		userName=name;
	}
	public void insertPassword(String pass) {
		pw=pass;		
	}
	public void insertcheck(String ck) {
		check=ck;
	}
	
	public void newFirstName(String fn)
	{
		newFirstName=fn;
		pn.firstNametextField.setText(newFirstName);
	}
	
	public void newLastName(String ln)
	{
		newLastName=ln;
		pn.lastNametextField.setText(newLastName);
	}
	
	public void createReaderFirstName(String name)
	{
		FirstName=name;
		
	}
	
	public void createReaderLastName(String name)
	{
		LastName=name;
	}
	
	public void createReaderPhone(String phoneNum)
	{
		phone=phoneNum;
	}
	
	public void createReaderMail(String mail)
	{
		email=mail;
	}
	
	public void createReaderId(String id)
	{
		this.id=id;
	}
	
	
	public void createReaderCard()
	{
		String[] s=new String[6];
		s[0]="CreateNewReaderCard";
		s[1]=id; 
		s[2]=FirstName;
		s[3]=LastName;
		s[4]=phone;
		s[5]=email;
		MainGUI.OBLClient.handleMessageFromClient(s);
		while(MainGUI.done==false);
	}
	
	
	
	public void update()
	{		
		pn.btnSubmit.doClick();
		sg.btnViewMyReaderCard.doClick();
		sg.btnUpdateDetails.doClick();
	}
	
	public boolean checkUpdateFirstName()
	{
		if(pn.firstNametextField.getText().contentEquals(check))
			return true;
		return false;		
	}
	public boolean checkUpdatePhone()
	{
		if(pn.phoneNumtextField.getText().contentEquals(check))
			return true;
		return false;		
	}
		
	public boolean checkUpdateLastName()
	{
		if(pn.lastNametextField.getText().contentEquals(check))
			return true;
		return false;		
	}
	
	
	public boolean checkUpdateEmail()
	{
		if(pn.eMailtextField.getText().contentEquals(check))
			return true;
		return false;		
	}
	
	
	
	public boolean checkUpdatePhoneNumber()
	{
		if(pn.phoneNumtextField.getText().contentEquals(check))
			return true;
		return false;		
	}
	
	
	public void newEmail(String email)
	{
		this.email=email;
		pn.eMailtextField.setText(email);
	}
	
	public void newPhone(String phoneNum)
	{
		phone=phoneNum;
		pn.phoneNumtextField.setText(phone);
	}
	
	
	public void insertOldPassword(String pw)
	{
		oldPassword=pw;
		pn.oldPwtextField.setText(oldPassword);
	}
	
	
	public void insertNewPassword(String pw)
	{
		newPassword=pw;
		pn.newPwtextField.setText(newPassword);
	}
	
	public void updatePassword()
	{
		pn.btnChangePassword.doClick();
	}
	
	public void setLoginPasswordField(String pw)
	{
		lg.passwordField.setText(pw);
	}
	
	public boolean login()
	{
		lg.textFieldLogin.setText(userName);
		MainGUI.done=false;
		lg.btnSubmit.doClick();

		
		while(MainGUI.done==false);
		Component x[]=MainGUI.panelButtons.getComponents();

		
		
		if(x[0].toString().contentEquals("SubscriberGUI"))
		{
			sg.btnUpdateDetails.doClick();
			return true;
		}
		return false;		
	}
	
	public void removeTestUser()
	{
		String[] s=new String[1];
		s[0]="DeleteTestUsers";
		MainGUI.OBLClient.handleMessageFromClient(s);
	}
	
	

}
